#!/bin/bash

#׼current dir
cur_dir=$(pwd)

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install moodle"
    exit 1
fi

# Check MemTotal
MemTotal=$( free -m | grep Mem | awk '{print  $2}' )
if [[  ${MemTotal} -lt 960 ]]; then
    echo -e "\033[31m Warning: \033[0m"
    echo "    Your Memory is: ${MemTotal} MB less than 1GB, can't install MariaDB ${MariaDB_Version}"
    exit 1
fi

# Check Nginx With memc-nginx-module
Memc=$1
if [ "${Memc}" = "" ]; then
    Nginx_Info=""
 elif echo  ${Memc} | grep -Eqi "memc" ; then
    Nginx_Info=" with memc-nginx-module"
    Memc="memc"
 else
    Memc=""
fi

. imoodle.conf
. conf/imoodle.conf
. include/main.sh
. include/end.sh
. include/mariadb.sh
. include/php.sh
. include/memcached.sh
. include/nginx.sh
. include/moodle.sh



[[ -s /root/moodle-install.log ]] && mv /root/moodle-install.log /root/moodle-install.last.log

Get_Dist_Name
if [ "${DISTRO}" = "unknow" ]; then
    Echo_Red "Unable to get Linux distribution name, or do NOT support the current distribution."
    exit 1
fi
clear
echo "+------------------------------------------------------------------------+"
echo "|     Moodle Service for Debian/CentOS Linux Server, Written by hongm    |"
echo "+------------------------------------------------------------------------+"
echo "|          A tool to auto-compile & install LNMP+Moodle on Linux         |"
echo "+------------------------------------------------------------------------+"
echo "|        For more information please visit http://hongm.emoodle.org      |"
echo "+------------------------------------------------------------------------+"


Moodle_Install()
{
    Print_APP_Ver
    Print_Sys_Info
    Press_Install
    Check_Hosts
    Check_Mirror
    if [ "${DISTRO}" = "CentOS" ]; then
        CentOS_Modify_Source
     elif [ "${DISTRO}" = "Debian" ]; then
        Debian_Modify_Source
    fi
    Set_Timezone
    if [ "$PM" = "yum" ]; then
        CentOS_InstallNTP
        CentOS_RemoveAMP
        CentOS_Dependent
     elif [ "$PM" = "apt" ]; then
        Deb_InstallNTP
        Deb_RemoveAMP
        Deb_Dependent
    fi
    Disable_Selinux
    Set_Iptables
    Check_Download
    Install_Autoconf
    Install_Cmake
    Install_Libiconv
    Install_Mhash
    Install_Freetype
    Install_Curl
    Install_Pcre
    Install_Icu4c
    Install_TCMalloc
    if [ "$PM" = "yum" ]; then
        CentOS_Lib_Opt
     elif [ "$PM" = "apt" ]; then
        Deb_Lib_Opt
    fi
    Install_MariaDB
    Install_PHP
    [[ "${Memc}" = "memc" ]] && Install_Memcached
    Install_Nginx
    Install_Moodle
    Check_Install
}

Moodle_Install 2>&1 | tee -a /root/moodle-install.log
